﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace PefStudio.Windows
{
    [Export(AbstractDocument.DockTabContextMenuContract, typeof(IMenuItem))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    sealed class TabContextMenuClose : AbstractMenuItem
    {
        public TabContextMenuClose()
        {
            ID = "Close";
            Text = "Close";
        }

        protected override void OnClick(EventArgs e)
        {
            ParentForm.Close();
        }
    }

    [Export(AbstractDocument.DockTabContextMenuContract, typeof(IMenuItem))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    sealed class TabContextMenuCloseAllButThis : AbstractMenuItem
    {
        public TabContextMenuCloseAllButThis()
        {
            ID = "CloseAllButThis";
            Text = "Close All But This";

            InsertRelativeToID = "Close";
        }

        protected override void OnClick(EventArgs e)
        {
            var parentDockDocument = ParentForm.ParentForm as AbstractDockDocument;
            if (parentDockDocument == null) return;

            for (int i = parentDockDocument.Documents.Count - 1; i >= 0; i--)
            {
                if (parentDockDocument.Documents[i] != ParentForm)
                    parentDockDocument.Documents[i].Close();
            }
        }

        public override void ConditionEvaluate()
        {
            var parentDockDocument = ParentForm.ParentForm as AbstractDockDocument;
            if (parentDockDocument == null) return;

            Enabled = parentDockDocument.Documents.Count != 1;
        }
    }

    [Export(AbstractDocument.DockTabContextMenuContract, typeof(IMenuItem))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    sealed class TabContextMenuCloseAll : AbstractMenuItem
    {
        public TabContextMenuCloseAll()
        {
            ID = "CloseAll";
            Text = "Close All";

            InsertRelativeToID = "CloseAllButThis";
        }

        protected override void OnClick(EventArgs e)
        {
            var parentDockDocument = ParentForm.ParentForm as AbstractDockDocument;
            if (parentDockDocument == null) return;

            for (int i = parentDockDocument.Documents.Count - 1; i >= 0; i--)
            {
                parentDockDocument.Documents[i].Close();
            }
        }
    }

    [Export(AbstractDocument.DockTabContextMenuContract, typeof(IMenuItem))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    sealed class TabContextMenuHorizontal : AbstractMenuItem
    {
        public TabContextMenuHorizontal()
        {
            ID = "Horizontal";
            Text = "Horizontal";

            InsertRelativeToID = "CloseAll";
            Image = Resources.Images.Tile_vertical;
            HasSeparator = true;
        }

        protected override void OnClick(EventArgs e)
        {
            var parentDockDocument = this.ParentForm.ParentForm as AbstractDockDocument;
            if (parentDockDocument == null) return;

            var prePane = parentDockDocument.Pane;
            for (int i = parentDockDocument.Documents.Count - 1; i >= 0; i--)
            {
                var doc = parentDockDocument.Documents[i];

                if (doc.DockPanel.ActiveContent != null)
                {
                    prePane = doc.DockPanel.ActiveContent.DockHandler.Pane;
                }
                else
                    continue;

                doc.DockTo(doc.DockPanel.DockPaneFactory.CreateDockPane(doc, prePane, DockAlignment.Bottom, 0.5, true), DockStyle.Fill, i);
            }
        }

        public override void ConditionEvaluate()
        {
            var parentDockDocument = ParentForm.ParentForm as AbstractDockDocument;
            if (parentDockDocument == null) return;

            Enabled = parentDockDocument.Documents.Count != 1;
        }
    }

    [Export(AbstractDocument.DockTabContextMenuContract, typeof(IMenuItem))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    sealed class TabContextMenuVertical : AbstractMenuItem
    {
        public TabContextMenuVertical()
        {
            ID = "Vertical";
            Text = "Vertical";
            Image = Resources.Images.Tile_horizontal;

            InsertRelativeToID = "Horizontal";
        }

        protected override void OnClick(EventArgs e)
        {
            var parentDockDocument = this.ParentForm.ParentForm as AbstractDockDocument;
            if (parentDockDocument == null)
                return;

            DockPane prePane = parentDockDocument.Pane;
            for (int i = parentDockDocument.Documents.Count - 1; i >= 0; i--)
            {
                var doc = parentDockDocument.Documents[i];

                if (doc.DockPanel.ActiveContent != null)
                {
                    prePane = doc.DockPanel.ActiveContent.DockHandler.Pane;
                }
                else
                    continue;

                doc.DockTo(doc.DockPanel.DockPaneFactory.CreateDockPane(doc, prePane, DockAlignment.Right, 0.5, true), DockStyle.Fill, i);
            }
        }

        public override void ConditionEvaluate()
        {
            var parentDockDocument = ParentForm.ParentForm as AbstractDockDocument;
            if (parentDockDocument == null) return;

            Enabled = parentDockDocument.Documents.Count != 1;
        }
    }
}
